<template>
    <div>
      <basic-container>
        <avue-crud ref="crud"
                   :page.sync="page"
                   :data="tableData"
                   :table-loading="tableLoading"
                   :option="tableOption"
                   @on-load="getList"
                   @search-change="searchChange"
                   @refresh-change="refreshChange">
          <template slot-scope="scope" slot="menu">
            <el-button type="text"
                       size="small"
                       plain
                       @click="jumpTodetail(scope.row)">详情</el-button>
            <el-button type="text"
                       size="small"
                       plain
                       @click="jumpToedit(scope.row)">编辑</el-button>
            <el-button type="text"
                       size="small"
                       plain
                       @click="del(scope.row)">删除</el-button>
          </template>
          <template slot="menuRight">
            <el-button type="primary" icon="el-icon-edit"  size="small" @click="jumpToAdd">新增打印机</el-button>
          </template>
        </avue-crud>
      </basic-container>
    </div>
</template>

<script>
    import {
      filterForm
    } from "../../../util/util";
    import {
      fetchList,
      deleteById
    } from '@/api/iot/cloudprintterminal'
    import {
      tableOption
    } from '@/const/crud/iot/cloudprintterminal'
    import router from "@/router/router"
    export default {
        name:'printer',
        data: function(){
            return {
              page: {
                total: 0, // 总页数
                currentPage: 1, // 当前页数
                pageSize: 20 // 每页显示多少条
              },
              tableData:[],
              tableOption:tableOption,
              tableLoading: false,
            }
        },
        methods:{
          getList(page, params={}) {
            this.tableLoading = true
            params.pmId = this.pmId;
            params.current = page.currentPage;
            params.size = page.pageSize;
            Object.assign(params, this.searchForm);
            fetchList(params).then(response => {
              if (response.data.code === 0) {
                this.tableData = response.data.data.records
                this.page.total = response.data.data.total
                this.tableLoading = false
              } else {
                this.tableData = [];
                this.tableLoading = false
                this.$message.error('请求失败')
              }
            }).catch(()=>{
              this.tableData = [];
              this.tableLoading = false
            })
          },
          /**
           * 搜索回调
           */
          searchChange(form, done) {
                this.getList(this.page, form);
                done();
            },
          /**
           * 刷新回调
           */
          refreshChange(info) {
    this.getList(this.page, this.filterForm(info.searchForm));
},
          //去新增
          jumpToAdd(){
            this.$router.push({path: '/equipment/clouDhandleAdd'})
          },
          //去详情
          jumpTodetail(row){
            let query = {
              res:JSON.stringify(row)
            };
            this.$router.push({path:'/equipment/clouDdetail',query:query})
          },
          //去编辑
          jumpToedit(row) {
            let query = {
              res:JSON.stringify(row)
            };
            this.$router.push({path: '/equipment/clouDedit',query:query})
          },
          //删除
          del(params) {
            this.$confirm('确认删除吗?', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning',
              center: true
            }).then(() => {
              deleteById(params).then(res=>{
                if(res.code=200){
                  this.$message.success("删除成功");
                  this.getList(this.page);
                }else{
                  this.$message.error("删除失败")
                }
              });
            }).catch(() => {

            });
          },
        }
    }
</script>

<style scoped>
</style>
